#include <Handler.h>

namespace _ServerHandle_ {
    const char *title_field = "title";
    const char *content_field = "content";
    const char *token_field = "token";
    const char *template_field = "template";
    const char *application_json = "application/json";
}

namespace _ServerHandle_ {
    const char *send_text_path = "/send";

    void send_text_GET(const httplib::Request &req, httplib::Response &res) {
        json cont;
        string token, title, content;
        SQLServer::Template temp = SQLServer::Template::txt;
        ErrorCode sent_res;

        //判断参数是否有token
        if (req.has_param(token_field)) {
            if (req.get_param_value(token_field).empty()) {
                Error2Json(ServerErrorCode::token_cannot_empty);
                goto out;
            }
            else
                token = req.get_param_value(token_field);
        }
        else {
            cont = Error2Json(ServerErrorCode::missing_token_in_request_param);
            goto out;
        }

        title = req.get_param_value(title_field);

        //判断参数是否有content
        if (req.has_param(content_field)) {
            content = req.get_param_value(content_field);
        }
        else {
            cont = Error2Json(ServerErrorCode::missing_content_in_request_param);
            goto out;
        }
        //判断参数content是否为空
        if (content.empty()) {
            cont = Error2Json(ServerErrorCode::content_cannot_empty);
            goto out;
        }


        //TODO 正常操作后

        if (req.has_param(template_field))
            temp = String2Template(req.get_param_value(template_field));

        if (temp == SQLServer::Template::NotFound){
            cont = Error2Json(ServerErrorCode::template_wrong);
            goto out;
        }

        sent_res = sqlServer->sent(title, content, token, temp);
        cont = Error2Json(sent_res);


        out:
        string res_js = cont.dump(4);
        res.set_content(res_js, application_json);
    }
}

/**在此添加处理函数*/
namespace ServerHandle {
    Handle_List handles{
            Handle{HandleType::GET,
                   send_text_path,
                   send_text_GET},


    };
}